package com.feri.ssm.service.impl;

import com.feri.ssm.dao.CourseDao;
import com.feri.ssm.entity.Course;
import com.feri.ssm.service.intf.CourseService;
import com.feri.ssm.vo.PageVo;
import com.feri.ssm.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao dao;

    @Override
    public R save(Course course) {
        if(dao.add(course)>0){
            return R.ok();
        }else {
            return R.fail("亲，网络异常，新增失败！");
        }
    }

    @Override
    public R update(Course course) {
        if(dao.update(course)>0){
            return R.ok();
        }else {
            return R.fail("亲，网络异常，修改失败！");
        }
    }

    @Override
    public R delete(int id) {
        if(dao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail("亲，网络异常，删除失败！");
        }
    }

    @Override
    public PageVo queryPage(int page, int limit) {
        //1.设置
        PageHelper.startPage(page, limit);
        //2.获取
        PageInfo<Course> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }
}
